package the.workshop.bu.dto.req;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import the.broccoli.auth.UserContext;
import the.workshop.bu.entity.Customer;
import the.workshop.bu.entity.CustomerTax;
import the.workshop.bu.entity.CustomerTel;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class CustomerModifyReq {

    private Integer id;

    /**
     * 客户名称
     */
    private String name;

    /**
     * 法人
     */
    private String legal;
    private String idNumber;
    private String wxNumber;

    private Integer star;
    private String tag;
    private String status;
    private Date followTime;
    private String remark;
    private LocalDateTime modifiedTime = LocalDateTime.now();
    private String modifiedId;
    private String modifiedName;
    /**
     * 公司地址
     */
    private String address;

    /**
     * 营业执照号码
     */
    private String certNumber;

    /**
     * 营业执照发送日期
     */
    private Date certDate;

    /**
     * 开户银行
     */
    private String bankName;

    /**
     * 对公账号
     */
    private String bankNumber;

    /**
     * 纳税等级
     */
    private String taxLevel;

    /**
     * 税务登录密码
     */
    private String taxPassword;

    /**
     * 对公流水年
     */
    private String billWater;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 所属银行（4大行）
     */
    private String bank4Name;

    /**
     * 4大行常用卡号
     */
    private String bank4Number;

    /**
     * 联系电话
     */
    private String tels;

    /**
     * 纳税情况
     */
    private String taxes;

    public Customer toCustomer() {
        Customer customer = new Customer();
        BeanUtils.copyProperties(this, customer);
        return customer;
    }

    public List<CustomerTel> toCustomerTels(Integer customerId) {
        if (!StringUtils.hasText(tels)) {
            return null;
        }

        List<CustomerTelAddReq> list = JSON.parseArray(tels, CustomerTelAddReq.class);

        List<CustomerTel> records = new ArrayList<>();
        CustomerTel record = null;
        for (CustomerTelAddReq tel : list) {
            record = new CustomerTel();
            record.setCustomerId(customerId);
            record.setTelNumber(tel.getTelNumber());
            record.setIsLegal(tel.getIsLegal());
            record.setIsInvalid(tel.getIsInvalid());
            records.add(record);
        }
        return records;
    }

    public List<CustomerTax> getCustomerTaxes() {
        if (!StringUtils.hasText(taxes)) {
            return null;
        }

        List<CustomerTax> list = JSON.parseArray(taxes, CustomerTax.class);
        for (CustomerTax ct : list) {
            ct.setCustomerId(this.id);
            ct.setCreatedId(UserContext.userId());
            ct.setCreatedName(UserContext.userName());
            ct.setCreatedTime(new Date());
        }
        return list;
    }
}
